<!--suppress ALL -->
<script lang="ts">
export default {
  name: "RepositoryCompEntityTable"
}
</script>

<template>
  <!-- 数据表单 -->
  <el-table
    ref="dataTableRef"
    v-loading="false"
    :data="dataState.entityList"
    border
    highlight-current-row
    @row-click="handleRowClick"
  >
    <!-- 展开列表 -->
    <el-table-column type="expand">
      <template #default="props">
        <el-input v-model="props.row.compParamJson" :autosize="{ minRows: 3, maxRows: 100 }" :readonly="true"
                  type="textarea"/>
      </template>
    </el-table-column>


    <el-table-column label="应用名称" min-width="150" prop="compParam.appName"/>

    <el-table-column align="center" label="应用类型" min-width="90" prop="conflictStatus">
      <template #default="scope">
        <el-tag v-if="scope.row.compParam.appType === 'service'" type="normal">服务</el-tag>
        <el-tag v-else-if="scope.row.compParam.appType === 'kernel'" type="warning">内核</el-tag>
        <el-tag v-else-if="scope.row.compParam.appType === 'system'" type="success">系统</el-tag>
        <el-tag v-else type="success">未定义</el-tag>
      </template>
    </el-table-column>

    <el-table-column :formatter="formatDate4elTableColumn" label="更新日期" prop="createTime" width="180"/>
    <el-table-column :formatter="formatDate4elTableColumn" label="更新日期" prop="updateTime" width="180"/>


    <!-- 上传对话框/下载对话框 -->
    <el-table-column align="center" label="操作" width="200">
      <template #default="scope">
        <el-button :icon="Edit" circle plain type="primary" @click.stop="handleUpdateDialog(scope.row)"/>
      </template>
    </el-table-column>

  </el-table>

  <!-- 分页工具条 -->
  <pagination v-if="dataState.total > 0" v-model:limit="dataState.queryParams.pageSize"
              v-model:page="dataState.queryParams.pageNum" :total="dataState.total"
              @pagination="handleQuery"/>
</template>

<script lang="ts" setup>
import {onMounted, reactive, toRefs} from "vue";
import {useRouter} from "vue-router";

import {
  RepoCompEntity,
  RepoCompQueryParam,
} from "@/api/repo/comp/local/types";
import {
  listCompEntityList,
} from "@/api/repo/comp/local";
import {ArrowRight, More, Edit, Delete} from '@element-plus/icons-vue';
import {formatDate4elTableColumn, formatDate4elTableColumn2} from '@/utils/formatter';
import {formatFileSize4elTableColumn, formatFileSize4elTableColumn2} from '@/utils/fileutil';


const emit = defineEmits(['rowClick', 'update', 'delete', 'deleteVersion', 'jarInfo', 'download', 'install']);// 定义组件的事件通知
const router = useRouter();

/**
 * 对外声明接口：父页面可以调用它
 */
defineExpose({refreshTable, getEntities, getSelects});

// 数据状态：定义
let dataState = reactive({
  // 页面转载状态
  loading: false,
  // 选中ID数组
  selects: [],
  // 记录总数
  total: 0,
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 组件仓库
  compRepo: '',
  // 组件类型
  compType: '',
  // 实体数据
  entityList: [] as RepoCompEntity[],
  // 查询参数
  queryParams: {pageNum: 1, pageSize: 10} as RepoCompQueryParam,
});

/**
 * 捕获列表中的选择变化
 * @taskParam selection
 */
function handleSelectionChange(selection: any) {
  dataState.selects = selection.map((item: any) => item);
  dataState.single = selection.length !== 1;
  dataState.multiple = !selection.length;
}

/**
 * 捕获列表中的行选择
 * @taskParam row
 */
function handleRowClick(row: any) {
  emit('rowClick', row);
}

function handleUpdateDialog(row: any) {
  emit('update', row);
}

/**
 * 捕获下载按钮
 * @taskParam row 行信息
 */
function handleDownloadPackage(row: any) {
  emit('download', 'table', 'download', row);
}

function handleInstallPackageDialog(row: any) {
  if (row.status < 2) {
    return;
  }

  emit('install', 'table', 'install', row);
}

function handleDeletePackageDialog(row: any) {
  if (row.status < 2) {
    return;
  }

  emit('delete', 'table', 'delete', row);
}


/**
 * 刷新表单
 */
function refreshTable(queryParams: RepoCompQueryParam) {
  dataState.queryParams = queryParams;

  handleQuery();
}

/**
 * 刷新页面信息
 */
function handleQuery() {
  dataState.queryParams.compRepo = dataState.compRepo;
  dataState.queryParams.compType = dataState.compType;
  listCompEntityList(dataState.queryParams).then(({data}) => {
    data.list.forEach(value => {
      value.compParamJson = JSON.stringify(value.compParam, null, 5);
    });

    dataState.entityList = data.list;
    dataState.total = data.total;
  });
}

/**
 * 获得选中的ID
 */
function getSelects(): any {
  return dataState.selects;
}

/**
 * 获得实体信息
 */
function getEntities(): any {
  return dataState.entityList;
}

/**
 * 响应页面安装：页面的初始化工作
 */
onMounted(() => {
  dataState.compRepo = 'local';
  dataState.compType = 'app-service';

  // 查询实体数据
  handleQuery();
});

</script>
